package cn.rulian.car.outbill.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rulian.base.auth.ann.Access;
import cn.rulian.base.common.dto.Message;
import cn.rulian.base.common.vo.Result;
import cn.rulian.car.outbill.bo.OutBill;
import cn.rulian.car.outbill.bo.OutBillFlow;
import cn.rulian.car.outbill.service.OutBillFlowService;
import cn.rulian.car.outbill.service.OutBillService;
import cn.rulian.car.outbill.vo.OutBillVo;

/**
 * 车辆外出单--回录
 */

@Access(authCode="outBill")
@Controller
@RequestMapping("/car/outbill/outBillBack")
public class OutBillBackController {
	
	@Autowired
	private OutBillService service;
	
	@Autowired
	private OutBillFlowService outBillFlowService; 
	
	//private final Logger log = LoggerFactory.getLogger(getClass());
	
	private final String path="car/outbill/outbillBack_"; //页面基本路径

	/**
	 * 页面基本路径
	 */
	private String getPath()
	{
		return path;
	}
	
	/**
	 * 列表页面
	 */
	@RequestMapping("/listPage")
	public String listPage() {
		return this.getPath()+"list";
	}

	/**
	 * 查询
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Result listData(OutBillVo vo) {

		vo.setDataSource(OutBill.DATASOURCE_APPLE);
		vo.setQueryForm("back");
		return service.queryBack(vo);
	}


	/**
	 * 详情页面
	 */
	@RequestMapping("/detailPage")
	public String detailPage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return "car/outbill/outbillApply_detail";
	}
	

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Message delete(Long ids[]) {
		Message rm = new Message();
		try {
			for(Long id:ids)
			{
				List<OutBillFlow> ls = outBillFlowService.findByOutBill_id(id);
				outBillFlowService.delete(ls); //删除流转
				service.delete(id);
			}
			
			rm.setMsg("删除成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("删除失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
}
